#include <accelerometer.h>
#include <display.h>
#include <error.h>
#include <h01.h>
#include <iic.h>

uint	steps;
uint	timeOfs;
uint	timer;
ushort	error;

static ubyte	acc,tap;

//------------------------------------------------------------------------------

static void boot(void){
	init(RCC_SYSCLK_Div2);
	initAccelerometer();
	displayOn();
	outHex(0x55aa);
	timer=0;
}
static void initAccelerometer(void){
	static ubyte ctrl[]={
		0x47,			// ctrl1: 50-Hz, enable xyz
		0xc5,			// ctrl2: normal filter on tap and int
		0x80,			// ctrl3: aoi1 on int1
		0x00,			// ctrl4: lsb, 2g
		0x00,			// ctrl5: fifo off, int not latched
		0x82,			// ctrl6: tap enabled, hi-active
	};
	static ubyte int1[]={
		0x2a,			// cfg: xyz up
		0,				// src: dummy
		0x00,			// ths: 0
		0x10,			// duration: 16
	};
	static ubyte tap[]={
		0x15,			// xfg: xyz single tapup
		0,				// src: dummy
		0x40,			// ths: 40
		0x01,			// timelimit: 1
	};
	initIIC();
	writeIIC(ADD_ACC,REG_CTRL1|0x80,ctrl,6);
	writeIIC(ADD_ACC,REG_INTCFG|0x80,int1,4);
	writeIIC(ADD_ACC,REG_TAPCFG|0x80,tap,4);
}

//------------------------------------------------------------------------------

void main(void){
	boot();
	while(1){
		if(acc){
			readIIC(ADD_ACC,REG_REFERENCE,&acc,1);
			outHex(acc);
			timer=0;
			acc=0;
		}
		if(tap){
			//outBar(19);
			tap=0;
			timer=0;
		}
		if(timer>4000){
			//ubyte c=0xb5;	// ctrl2: hp-filter on tap and int
			//writeIIC(ADD_ACC,REG_CTRL2,&c,1);
			PWR_WakeUpPinCmd(ENABLE);
			PWR_EnterSTANDBYMode();
		}else if(timer>500)
			outBar(1);
	}
}